package com.dlc.shop.search.common.manager;

import com.dlc.shop.bean.param.EsProductParam;
import com.dlc.shop.common.enums.EsRenovationProductSortEnum;
import com.dlc.shop.search.common.service.SearchProductService;
import com.dlc.shop.service.OrderItemService;
import com.dlc.shop.service.ProdCommService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.Objects;


/**
 * @author FrozenWatermelon
 * @date 2020/11/16
 */
@Component
@AllArgsConstructor
public class ProductSearchManager {


    private final ProdCommService prodCommService;
    private final OrderItemService orderItemService;
    private final SearchProductService searchProductService;

    public void addRenovationSpuCache(String key, EsProductParam productSearchDTO, Long shopId, Integer expireTime) {
        if(Objects.isNull(productSearchDTO.getEsTimeRange()) && Objects.isNull(productSearchDTO.getEsRenovationSpuSort())){
            return;
        }
        // 计算时间
        int dayNum = 0;
        switch (productSearchDTO.getEsTimeRange()){
            case 1:
                dayNum = 365;
                break;
            case 2:
                dayNum = 90;
                break;
            case 3:
                dayNum = 30;
                break;
            case 4:
                dayNum = 7;
                break;
            default:
                break;
        }
        // 1.获取根据条件的商品ids
        if(Objects.equals(productSearchDTO.getEsRenovationSpuSort(), EsRenovationProductSortEnum.COMMENT_NUM_DESC.value())
                || Objects.equals(productSearchDTO.getEsRenovationSpuSort(),EsRenovationProductSortEnum.COMMENT_NUM_ASC.value())){
            // 获取时间范围内的评论数排序,放入redis
            prodCommService.getCommNumRankSpuIdsByShopIdAndTime(key, shopId, dayNum, expireTime, productSearchDTO.getEsRenovationSpuSort(), productSearchDTO.getPrimaryCategoryId());
        }else {
            // 获取时间范围内的销量排序，放入redis
            orderItemService.getSoldNumRankByShopIdAndTime(key, shopId, dayNum, expireTime, productSearchDTO.getEsRenovationSpuSort(), productSearchDTO.getPrimaryCategoryId());
        }
    }
}
